package com.neusoft.elmboot.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.elmboot.Vo.PermVo;
import com.neusoft.elmboot.mapper.PermissionMapper;
import com.neusoft.elmboot.po.Permission;
import com.neusoft.elmboot.service.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService  {

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Permission record) {
        return permissionMapper.insert(record);
    }

    @Override
    public int insertSelective(Permission record) {
        return permissionMapper.insertSelective(record);
    }

    @Override
    public Permission selectByPrimaryKey(Integer id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Permission record) {
        return permissionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Permission record) {
        return permissionMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<PermVo> selectAllPermission() {
        List<Permission> permList = permissionMapper.selectList();

        List<PermVo> voList = new ArrayList<>();

        Map<Integer, PermVo> map = new HashMap<>();

        for (Permission perm : permList) {
            Integer id = perm.getId();
            Integer parentId = perm.getParentId();

            PermVo permVo = null;

            if (parentId == 0) {
                // 父节点
                permVo = map.get(id);
                if (permVo == null) {
                    permVo = new PermVo();
                    permVo.setId(id+"");
                    permVo.setLabel(perm.getPermName());
                    permVo.setChildren(new ArrayList<PermVo>());
                    map.put(id,permVo);
                }

            } else {
                // 子节点
                permVo = map.get(parentId);
                if (permVo == null) {
                    permVo = new PermVo();
                    permVo.setId(parentId+"");
                    permVo.setLabel(perm.getPermName());
                    permVo.setChildren(new ArrayList<PermVo>());
                    map.put(id,permVo);
                }
                List<PermVo> children = permVo.getChildren();
                PermVo childPermVo = new PermVo(perm.getId()+"",perm.getPermName(),
                        perm.getPermApi(),null);
                children.add(childPermVo);
            }
            if (!voList.contains(permVo)) {
                voList.add(permVo);
            }


        }


        return voList;
    }

}
